package com.lovo.sqlatm;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Login extends JPanel implements ActionListener {
    private Icon log = Util.getIcon("/img/buttonLand.jpg");
    private Icon enroll = Util.getIcon("/img/buttonLogin.jpg");
    private Test0 frame;
    private JButton jButton = new JButton(log);
    private JTextField use = new JTextField();
    private JPasswordField pass = new JPasswordField();
    private String pas;
    private String us;
    private String ue;
    private String ps;
    private int number = 3;

    public String getUe() {
        return ue;
    }

    public void setUe(String ue) {
        this.ue = ue;
    }

    public Login(Test0 frame) {
        this.frame = frame;
        this.setLayout(null);
        //账号
        JLabel title = new JLabel("账号：");
        title.setBounds(314, 338, 40, 20);
        this.add(title);
        JLabel title1 = new JLabel("密码：");
        title1.setBounds(314, 388, 40, 20);
        this.add(title1);
        //账号输入
        use.setBounds(354, 338, 140, 20);
        use.addActionListener(this);
        this.add(use);

        //密码输入
        pass.setBounds(354, 388, 140, 20);
        use.addActionListener(this);
        this.add(pass);


        //登录按钮
        jButton.setBounds(324, 468, 140, 50);
        jButton.addActionListener(this);
        this.add(jButton);


        //注册按钮
        JButton jButton1 = new JButton(enroll);
        jButton1.setBounds(494, 468, 140, 50);
        this.add(jButton1);
        //透明的
        this.setOpaque(false);
    }



    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jButton) {
            us = use.getText();
            pas = pass.getText();

            JdbcUtil jdbcUtil = new JdbcUtil();
            Uesr uesr = jdbcUtil.selectNameAndPwd(us, pas);
            if (uesr!=null){
                frame.setUesr(uesr);
                JOptionPane.showMessageDialog(this, "登录成功,欢迎你");
                frame.switchPanel("menu");
                return;

            }
            if (number == 0) {
                JOptionPane.showMessageDialog(this, "错误3次以上，卡片冻结");
                System.exit(0);
            } else {
                JOptionPane.showMessageDialog(this, "登录失败");
                number--;
            }

        }
    }

}
